package fileSend;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author GON
 */
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class reciever {

    private final Socket socket;

    public reciever(Socket socket) {
        this.socket = socket;
    }

    public void receiveFile(File file) throws IOException {
        byte[] b = new byte[1024];
        int len = 0;
        int bytcount = 1024;
        FileOutputStream inFile = new FileOutputStream(file);
        InputStream is = socket.getInputStream();
        BufferedInputStream in2 = new BufferedInputStream(is, 1024);
        while ((len = in2.read(b, 0, 1024)) != -1) {
            bytcount = bytcount + 1024;
            inFile.write(b, 0, len);
        }
        socket.shutdownInput();
        System.out.println("Bytes Writen : " + bytcount);

        // Sending the response back to the client.
        /*
        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
        oos.flush();
        oos.writeObject("ok");
        System.out.println("Message sent to the client is " + "ok");
         * 
         */

        in2.close();
        inFile.close();
    }

    public static void main(String[] args) throws IOException {
        ServerSocket listen = new ServerSocket(10000);
        Socket socket = listen.accept();
        reciever server = new reciever(socket);
        server.receiveFile(new File("test-copy.txt"));
    }
}
